# coding=utf-8

import os

from docx import Document
from docx.shared import Pt

from django.conf import settings

BASE_DIR = settings.BASE_DIR


class DictToDocx:
    """将信息存到word中"""
    def __init__(self, data: dict):
        self.data_list = data.get('data_list')
        self.document = Document()

    def to_docx(self, pk, prefix, username):
        for dic in self.data_list:
            head = dic.get('head')
            data = dic.get('data')
            self.document.add_heading(head, 0)
            for key, value in data.items():
                p = self.document.add_paragraph(f'{key}:{value}', style='ListBullet')

        # path = os.path.join(BASE_DIR, 'media/resume_upload/', f'{pk}-{self.username}个人简历.docx')
        path = os.path.join(BASE_DIR, 'media/resume_upload/', f'{pk}-{username}个人简历{prefix}')
        # if not os.path.exists(path):
        self.save(path)
        print(f'{username}个人简历.docx保存成功')
        return path

    def save(self, name):
        self.document.save(name)

    # def get_content(self):
    #     content = ''
    #     for paragraph in self.document.paragraphs:
    #         content += paragraph.text
    #     return content


class LogDictDocx:
    def __init__(self):
        self.document = Document()

    def add_info_table(self, dic: dict, username):
        """
        基本信息
        """
        self.document.add_heading(f'{username}个人简历', level=0)
        self.add_logo()
        self.document.add_heading(dic.get('head'), level=1)
        data = dic.get('data')
        keys = list(data.keys())
        values = list(data.values())
        cols = (len(keys) + 1) // 2
        table = self.document.add_table(cols=cols, rows=cols)
        rows0 = table.column_cells(0)
        rows1 = table.column_cells(1)
        rows2 = table.column_cells(2)
        rows3 = table.column_cells(3)
        print(keys)
        print(values)
        for index, (r1, r2, r3, r4) in enumerate(zip(rows0, rows1, rows2, rows3)):
            r1.text = keys[index] + ':'
            r1.vertical_alignment = 0
            print(values[index])
            r2.text = str(values[index])
            r3.text = keys[index + 3] + ':'
            r4.text = values[index + 3]

    def add_introduce(self, text):
        """
        自我介绍
        """
        self.document.add_heading('自我评价', level=1)
        self.document.add_paragraph(text=text)

    def add_work_expirience(self, lst: list):
        """添加工作经历"""
        cols = 3
        rows = len(lst)
        self.document.add_heading('工作经历')
        work_table = self.document.add_table(cols=cols, rows=rows)
        for i in range(rows):
            for j in range(cols):
                work_table.cell(i, j).text = lst[i][j]

    def add_project(self, pro_dic_lst: list):
        self.add_logo()
        self.document.add_heading('项目经验', level=1)
        for pro in pro_dic_lst:
            project_name = pro.get('项目名')
            project_brief = pro.get('项目简介')
            p_name = self.document.add_paragraph(text='项目名:', style='List Number')
            p_name.add_run(text=project_name)
            p_brief = self.document.add_paragraph(text='项目简介:', style='List Bullet')
            p_brief.add_run(text=project_brief)

    def add_logo(self):
        logo_path = os.path.join(BASE_DIR, 'utils', 'logo.png')
        self.document.add_picture(logo_path, width=Pt(160), height=Pt(45))

    def save(self, path):
        self.document.save(path)